<?php
namespace yan\xss_sql_filter;
use Yii;
use yii\base\InvalidConfigException;
use yii\validators\IpValidator;
use yii\web\RequestParserInterface;
use yii\web\HttpException;

class InjectRequest extends \yii\web\Request{
    private $_bodyParams;
    public function checkInject($_bodyParams){
        if(!empty($_bodyParams)){
            if(is_string($_bodyParams)){
                if(XssAndSqlFilter::checkXSSAndSql($_bodyParams)){
                    throw new HttpException(403, 'Error input data');
                }
            }elseif(is_array($_bodyParams)){
                foreach($_bodyParams as $key => $val){
                    if(is_array($val)){
                        foreach($val as $k => $v){
                            $this->checkInject($v);
                        }
                    }else if(!empty($val)){
                        if(XssAndSqlFilter::checkXSSAndSql($val)){
                            throw new HttpException(403, 'Error input data');
                        }
                    }
                    
                }
            }
        }
    }
    public function getBodyParams()
    {
        if ($this->_bodyParams === null) {
            if (isset($_POST[$this->methodParam])) {
                $this->_bodyParams = $_POST;
                $this->checkInject($this->_bodyParams);
                unset($this->_bodyParams[$this->methodParam]);
                return $this->_bodyParams;
            }

            $rawContentType = $this->getContentType();
            if (($pos = strpos((string)$rawContentType, ';')) !== false) {
                // e.g. text/html; charset=UTF-8
                $contentType = substr($rawContentType, 0, $pos);
            } else {
                $contentType = $rawContentType;
            }

            if (isset($this->parsers[$contentType])) {
                $parser = Yii::createObject($this->parsers[$contentType]);
                if (!($parser instanceof RequestParserInterface)) {
                    throw new InvalidConfigException("The '$contentType' request parser is invalid. It must implement the yii\\web\\RequestParserInterface.");
                }
                $this->_bodyParams = $parser->parse($this->getRawBody(), $rawContentType);
            } elseif (isset($this->parsers['*'])) {
                $parser = Yii::createObject($this->parsers['*']);
                if (!($parser instanceof RequestParserInterface)) {
                    throw new InvalidConfigException('The fallback request parser is invalid. It must implement the yii\\web\\RequestParserInterface.');
                }
                $this->_bodyParams = $parser->parse($this->getRawBody(), $rawContentType);
            } elseif ($this->getMethod() === 'POST') {
                // PHP has already parsed the body so we have all params in $_POST
                $this->_bodyParams = $_POST;
            } else {
                $this->_bodyParams = [];
                mb_parse_str($this->getRawBody(), $this->_bodyParams);
            }
        }

        $this->checkInject($this->_bodyParams);
        return $this->_bodyParams;
    }
}